package com;


import java.io.*;
import java.util.*;
/**
 * @auther Macrydy
 * @date 2020/7/17 9:26
 */
public class Demo1 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        TreeSet<Student> set=new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = o2.getTotal()-o1.getTotal();
                result = result == 0 ? o2.getName().compareTo(o1.getName()) : result;
                return result;
            }
        });

        
        for (int i = 1; i <=3; i++) {
            System.out.println("请录入第"+i+"个学生信息：");
            System.out.println("姓名：");
            String name = sc.nextLine();
            System.out.println("语文成绩：");
            String language = sc.nextLine();
            System.out.println("数学成绩：");
            String math = sc.nextLine();
            System.out.println("英语成绩：");
            String english = sc.nextLine();

            int total=Integer.parseInt(language)+Integer.parseInt(math)+Integer.parseInt(english);
            set.add(new Student(name, Integer.parseInt(language),
                    Integer.parseInt(math),
                    Integer.parseInt(english),
                    total));

            BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt"));
            bw.write("姓名，语文成绩，数学成绩，英语成绩");
            bw.newLine();
            for (Student student : set) {
                bw.write(student.toString());
                bw.newLine();
                bw.flush();
            }
            bw.close();
        }
        BufferedReader br = new BufferedReader(new FileReader("student.txt"));
        String len;
        while ((len = br.readLine()) != null) {
            System.out.println(len);
        }

        br.close();
    }
}
class Student{
    private String name;
    private int language;
    private int math;
    private int english;
    private int total;

    public Student(String name, int language, int math, int english, int total) {
        this.name = name;
        this.language = language;
        this.math = math;
        this.english = english;
        this.total = total;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLanguage() {
        return language;
    }

    public void setLanguage(int language) {
        this.language = language;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public Student() {
    }

    @Override
    public String toString() {
        return  name+","+language+","+math+","+english;
    }
}